// pages/users/activation/index.js
const {
  cardActivation,
  cardActive
} = require('../../../api/api')
const {
  filterObj,
  checkLogin,
  message
} = require('../../../utils/util')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    mobile: '',
    cardPassword: '',
    cardNumber: '',
    initData: {},
    showModal: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (options.cardNumber) {
      this.setData({
        cardNumber: options.cardNumber,
        cardPassword: options.cardPassword
      })
    }
    this.setData({
      mobile: wx.getStorageSync('userInfo').mobile
    })
  },
  inputChange(e) {
    const {
      type
    } = e.currentTarget.dataset
    this.setData({
      [type]: e.detail.value
    })
  },
  async callback() {
    const {params} = this.data
    checkLogin().then(() => {
      if (!params.cardNumber) {
        wx.showToast({
          title: '请输入会员卡号',
          icon: 'none'
        })
        return
      } else if (!params.cardPassword) {
        wx.showToast({
          title: '请输入密码',
          icon: 'none'
        })
        return
      } else {
        if (!wx.getStorageSync('subjectInfo')) {
          this.setData({
            showModal: true
          })
          return
        }
        this.doActiveCard()
      }
    }).catch((e) => {
      // wx.showModal({
      //   title: '是否跳转至登录页',
      //   content: '需要您登录后，才能激活',
      //   success: function (res) {
      //     if (res.confirm) {
      //       wx.navigateTo({
      //         url: '/pages/login/index',
      //       })
      //     }
      //   }
      // })
    })
  },
  async doActiveCard() {
    let _self=this
    const {
      cardNumber,
      cardPassword
    } = this.data
    const {
      score,
      ranking,
      year,
      firstSelectSubjects,
      secondSelectSubjects,
      provinceId,
      province
    } = wx.getStorageSync('subjectInfo')
    var params = {
      provinceId,
      province,
      score,
      ranking,
      firstSelect: firstSelectSubjects,
      secondSelect: secondSelectSubjects,
      // year,
      cardNumber,
      cardPassword
    }
    params = filterObj(params)
    const result = await cardActive(params)
    if (result.success) {
      wx.showToast({
        title: result.message,
        icon: 'none'
      })
      _self.setData({
        cardPassword: '',
        cardNumber: '',
        showModal: false,
      })
      setTimeout(() => {
        wx.navigateBack()
      }, 1000);
    }

  },
  sendMessage(e) {
    var temIds = ['VTJr-PYcl78g4cmSM3WdT95GBxWTRpPmG1ArKkRQGcY']
     this.setData({
      params:e.detail.value
     })
    let self = this;
    message(false, temIds, '', self.callback.bind(self))
  },
 
  async onLoadFunc(e) {
    this.doActiveCard()
  },
  scan() {
    var that = this
    wx.scanCode({
      onlyFromCamera: true,
      success: function (res) {
        console.log(res)
        let value = getApp().getQueryVariable(res.path, 'scene')
        if (value.scene.indexOf('student') > -1) {
          const scene = value.scene.split('_')[1]
          wx.navigateTo({
            url: '/pages/auth/index?scene=' + scene,
          })
          return
        }
        let qrcode = value.scene.split('_')
        that.setData({
          cardNumber: qrcode[0],
          cardPassword: qrcode[1],
        })
      },
      fail: function (error) {

      }
    })
  },
})
